package osgi.common.services.client;

import java.lang.reflect.InvocationTargetException;
import java.util.Dictionary;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.osgi.service.component.ComponentContext;

import osgi.common.services.EServiceState;
import osgi.common.services.LoggerHelper;
import osgi.common.services.util.Util;




public abstract class AbstractService implements ICommonService {
	
	
	/**
	 * Logger for this class
	 */
	protected Logger logger;

	protected EServiceState state;
	
	protected String        serviceName;

	//@SuppressWarnings("rawtypes")
	protected Map<String, ? extends Object> configProperties;
	
	protected ComponentContext context;
		

	
	
	
	public AbstractService() {
		super();
		
		logger = LoggerHelper.getLogger(this.getLoggerName());
		
		//configProperties = new HashMap();
		
	}


	@Override
	public void activate(ComponentContext context) {
		logger.info("activate(ComponentContext) - start");
		
		this.context = context;
		
		try {
			
			loadConfigProperties(context.getProperties());
			
		} catch (IllegalAccessException e) {
			logger.error("activate(ComponentContext)", e);

			e.printStackTrace();
		} catch (InvocationTargetException e) {
			logger.error("activate(ComponentContext)", e);

			e.printStackTrace();
		}
		
		this.setServiceState(EServiceState.ACTIVE);

		logger.info("activate(ComponentContext) - end");
	}


	@SuppressWarnings("rawtypes")
	protected void loadConfigProperties(Dictionary properties) throws IllegalAccessException, InvocationTargetException {
		logger.info("loadConfigProperties(Dictionary) - start");
		
		this.configProperties = Util.dictionaryToMap(properties);
		
		BeanUtils.populate(this, this.configProperties);
		
		logger.warn("Config Parameters :");
		for (String ch : configProperties.keySet()) {			
			logger.info("   ["+ch+" = "+this.getConfigProperty(ch)+"]");	
		}
		
		
		logger.info("loadConfigProperties(Dictionary) - end");
	}


	@Override
	public void deactivate(ComponentContext context) {
		this.setServiceState(EServiceState.INACTIVE);
		
	}


	@Override
	public String getLoggerName() {		
		return this.getClass().getName();
	}

	
	@Override
	public EServiceState getServiceState() {
		return this.state;
	}


	@Override
	public void setServiceState(EServiceState state) {
		this.state = state;		
	}
	
	
	@Override
	public String getServiceName() {		
		return this.serviceName;
	}


	@Override
	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;		
	}


	public Map<String, ? extends Object> getConfigProperties() {
		return this.configProperties;
	}


	public void setConfigProperties(Map<String, ? extends Object> configProperties) {
		this.configProperties = configProperties;
	}
	
	
	public Object getConfigProperty(String propertyName){
		
		Object obj = null;
		
		if(configProperties != null){
			obj = this.configProperties.get(propertyName);
		}
		
		return obj;
		
	}
	
}
